<?php
class Usergroup extends AppModel {

	var $name = 'Usergroup';
	var $hasAndBelongsToMany = array(
		'Module' => array(
			'className' => 'Module',
			'joinTable' => 'usergroup_modules',
			'foreignKey' => 'usergroup_id',
			'associationForeignKey' => 'module_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => 'position ASC',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);

	function getAuthorizedController($id){
		$this->recursive = 2;
		$group = $this->find('first',array('conditions'=>array('id'=>$id)));
		if(!empty($group['Module'])){
			$result = array();
			foreach($group['Module'] as $g){
				if(!empty($g['Menu'])){
					foreach($g['Menu'] as $menu){
						$result[] = str_replace('/','',$menu['link']);
					}
				}
			}
			$result[] = 'home';
			return $result;
		}
		return null;
	}

	function getAuthorizedModule($id){
		$this->recursive = 2;
		$group = $this->find('first',array('conditions'=>array('id'=>$id)));
		if(!empty($group['Module'])){
			return $group['Module'];
		}
		return null;
	}
}
?>
